package com.taylor.community.controller;

import com.taylor.community.entity.Event;
import com.taylor.community.entity.Page;
import com.taylor.community.entity.User;
import com.taylor.community.event.EventProducer;
import com.taylor.community.service.FollowService;
import com.taylor.community.service.UserService;
import com.taylor.community.util.CommunityConstant;
import com.taylor.community.util.CommunityUtil;
import com.taylor.community.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class FollowController implements CommunityConstant {
    @Autowired
    private FollowService followService;
    @Autowired
    private HostHolder hostHolder;
    @Autowired
    private UserService userService;
    @Autowired
    private EventProducer eventProducer;
    @RequestMapping(path = "/follow" ,method = RequestMethod.POST)
    @ResponseBody
    public String follow(int entityType,int entityId){
        User user = hostHolder.getUser();
        followService.follow(user.getId(),entityType,entityId);
        //触发关注事件 系统给被关注者发送通知
        Event event = new Event()
                .setTopic(TOPIC_FOLLOW)
                .setUserId(hostHolder.getUser().getId())
                .setEntityUserId(entityType)
                .setEntityId(entityId)
                .setEntityUserId(entityId);
        eventProducer.fireEvent(event);
        return CommunityUtil.getJSONString(200,"已关注!");
    }
    @RequestMapping(path = "/unfollow" ,method = RequestMethod.POST)
    @ResponseBody
    public String unFollow(int entityType,int entityId){
        User user = hostHolder.getUser();
        followService.unFollow(user.getId(),entityType,entityId);
        return CommunityUtil.getJSONString(200,"已取消关注!");
    }

    /**
     * 访问的目标用户的关注的人的列表
     * @param userId
     * @param page
     * @param model
     * @return
     */
    @RequestMapping(path = "/followees/{userId}",method = RequestMethod.GET)
    public String getFollowees(@PathVariable("userId")int userId, Page page, Model model){
        //防止恶意访问
        User user = userService.findUserById(userId);
        if(user == null){
            throw new RuntimeException("该用户不存在！");
        }
        //
        model.addAttribute("user",user);
        page.setLimit(5);
        page.setPath("/followees"+userId);
        page.setRows((int) followService.findFolloweeCount(userId,ENTITY_TYPE_USER));
        //获取该用户关注对象的id
        List<Map<String,Object>> userList = followService.findFollowees(userId,page.getOffset(),page.getLimit());
        if(userList != null){
            for (Map<String,Object> map : userList){
                User u = (User) map.get("user");
                //判断当前登录用户所访问的用户的关注列表中是否有自己已经关注的对象
                //注意传入的是目标用户的id
                map.put("hasFollowed",hasFollowed(u.getId()));

            }

        }
        model.addAttribute("users",userList);
        return "/site/followee";
    }
    @RequestMapping(path = "/followers/{userId}",method = RequestMethod.GET)
    public String getFollowers(@PathVariable("userId")int userId, Page page, Model model){
        //防止恶意访问
        User user = userService.findUserById(userId);
        if(user == null){
            throw new RuntimeException("该用户不存在！");
        }
        //
        model.addAttribute("user",user);
        page.setLimit(5);
        page.setPath("/followers"+userId);
        page.setRows((int) followService.findFollowerCount(userId,ENTITY_TYPE_USER));
        //该用户粉丝的id
        List<Map<String,Object>> userList = followService.findFollowers(userId,page.getOffset(),page.getLimit());
        if(userList != null){
            for (Map<String,Object> map : userList){
                User u = (User) map.get("user");
                //判断当前登录用户所访问的用户的粉丝列表中是否有自己已经关注的对象
                //注意传入的是目标用户的id
                map.put("hasFollowed",hasFollowed(u.getId()));

            }
        }
        model.addAttribute("users",userList);
        return "/site/follower";
    }
    public boolean hasFollowed(int userId){
        if(hostHolder.getUser() == null){
            return false;
        }
        return followService.hasFollowed(hostHolder.getUser().getId(),ENTITY_TYPE_USER,userId);
    }
}
